package com.smarthome;

import org.apache.http.HttpResponse;
import com.smarthome.R;
import com.smarthome.tools.HttpUrlConnection7;
import com.smarthome.tools.UrlDefines;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TV_Controller extends Activity implements
		HttpUrlConnection7.onResponseReceivedListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tv_controller);

		findViewsAndSetListens();

	}

	protected void onResponseReceived(HttpResponse httpResponse) {
		// TODO Auto-generated method stub
		String resultString = HttpUrlConnection7
				.getStringFromHttpResponce(httpResponse);

		Toast.makeText(getApplicationContext(), resultString, Toast.LENGTH_SHORT)
				.show();
	}

	private void findViewsAndSetListens() {
		// TODO Auto-generated method stub
		setMyButtonListener(R.id.button_0, UrlDefines.tv.TV_0);
		setMyButtonListener(R.id.button_1, UrlDefines.tv.TV_1);
		setMyButtonListener(R.id.button_2, UrlDefines.tv.TV_2);
		setMyButtonListener(R.id.button_3, UrlDefines.tv.TV_3);
		setMyButtonListener(R.id.button_4, UrlDefines.tv.TV_4);
		setMyButtonListener(R.id.button_5, UrlDefines.tv.TV_5);
		setMyButtonListener(R.id.button_6, UrlDefines.tv.TV_6);
		setMyButtonListener(R.id.button_7, UrlDefines.tv.TV_7);
		setMyButtonListener(R.id.button_8, UrlDefines.tv.TV_8);
		setMyButtonListener(R.id.button_9, UrlDefines.tv.TV_9);
		setMyButtonListener(R.id.button_power, UrlDefines.tv.TV_POWER);
		setMyButtonListener(R.id.button_menu, UrlDefines.tv.TV_MENU);
		setMyButtonListener(R.id.button_mute, UrlDefines.tv.TV_MUTE);
		setMyButtonListener(R.id.button_sound_minus,
				UrlDefines.tv.TV_SOUNDMINUS);
		setMyButtonListener(R.id.button_sound_plus, UrlDefines.tv.TV_SOUNDPLUS);
		setMyButtonListener(R.id.button_channel_minus,
				UrlDefines.tv.TV_CHANNELMINUS);
		setMyButtonListener(R.id.button_channel_plus,
				UrlDefines.tv.TV_CHANNELPLUS);
		setMyButtonListener(R.id.button_double, UrlDefines.tv.TV_DOUBLE);

		HttpUrlConnection7.Instance().setOnResponseReceivedListener(
				new HttpUrlConnection7.onResponseReceivedListener() {

					@Override
					public void onHttpResponseReceived(HttpResponse httpResponse) {
						// TODO Auto-generated method stub
						onResponseReceived(httpResponse);
					}
				});

	}

	private void setMyButtonListener(int buttonId, int cmd) {
		// TODO Auto-generated method stub
		final int f_cmd = cmd;

 		Button button = (Button) findViewById(buttonId);
		button.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(f_cmd == UrlDefines.tv.TV_DOUBLE){
					Intent intent=new Intent(TV_Controller.this,CoverFlowActivity.class);
					startActivity(intent);
				}else {
					HttpUrlConnection7.Instance().connectToServerWithUser(
							f_cmd);
				}
			}
		});
	}

	@Override
	public void onHttpResponseReceived(HttpResponse httpResponse) {
		// TODO Auto-generated method stub
		String text = HttpUrlConnection7
				.getStringFromHttpResponce(httpResponse);
		Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
	}
}
